import cv2
import numpy as np

lower = np.array([10, 10, 46])
upper = np.array([180, 45, 150])


frame = cv2.imread(str(1)+'.jpg')   # 这里数字改成对应照片

frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# 要查看某一像素的HSV值，取消这一句注释调整，第一个[]为y，第二个[]为x
# print(hsv[][])
mask = cv2.inRange(hsv, lower, upper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.GaussianBlur(mask, (3, 3), 0)
res = cv2.bitwise_and(frame, frame, mask=mask)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)[-2]

if len(cnts) > 0:
    cnt = max(cnts, key=cv2.contourArea)
    (x, y), radius = cv2.minEnclosingCircle(cnt)
    print(hsv[int(y)][int(x)])
    cv2.circle(frame, (int(x), int(y)), int(radius), (255, 0, 255), 2)
    cv2.imshow('circle', frame)
    print("success")

else:
    pass


cv2.waitKey()
cv2.destroyAllWindows()
